package com.mdinic.blogovi.model;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

@Component
@Configurable
@RooDataOnDemand(entity = Person.class)
public class PersonDataOnDemand {

    @Autowired
    BlogTypeDataOnDemand blogTypeDataOnDemand;

    private final Random rnd = new SecureRandom();

    private List<Person> data;

    public void setAutoApprove(Person obj, int index) {
        Boolean autoApprove = Boolean.TRUE;
        obj.setAutoApprove(autoApprove);
    }

    public void setComments(Person obj, int index) {
        Integer comments = new Integer(index);
        obj.setComments(comments);
    }

    public void setPosted(Person obj, int index) {
        Integer posted = new Integer(index);
        obj.setPosted(posted);
    }

    public void setPromoted(Person obj, int index) {
        Integer promoted = new Integer(index);
        obj.setPromoted(promoted);
    }

    public void setVotes(Person obj, int index) {
        Integer votes = new Integer(index);
        obj.setVotes(votes);
    }

    public void setVotesReceived(Person obj, int index) {
        Integer votesReceived = new Integer(index);
        obj.setVotesReceived(votesReceived);
    }

    public Person getNewTransientPerson(int index) {
        Person obj = new Person();
        obj.setAutoApprove(false);
        obj.setComments(0);
        obj.setJoined(new Date());
        obj.setPosted(0);
        obj.setVotesReceived(0);
        obj.setVotes(0);
        obj.setPromoted(0);
        setBirthday(obj, index);
        setBlog(obj, index);
        setConfirmed(obj, index);
        setEmail(obj, index);
        setFirstname(obj, index);
        setJoined(obj, index);
        setLastname(obj, index);
        setNickname(obj, index);
        setPassword(obj, index);
        setPicture(obj, index);
        setRegistrationCode(obj, index);
        setWebsite(obj, index);
        return obj;
    }

    public void setBirthday(Person obj, int index) {
        Date birthday = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
                Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(
                Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(
                Calendar.SECOND)
                + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setBirthday(birthday);
    }

    public void setBlog(Person obj, int index) {
        String blog = "blog_" + index;
        if (blog.length() > 256) {
            blog = blog.substring(0, 256);
        }
        obj.setBlog(blog);
    }

    public void setConfirmed(Person obj, int index) {
        Boolean confirmed = Boolean.TRUE;
        obj.setConfirmed(confirmed);
    }

    public void setEmail(Person obj, int index) {
        String email = "foo" + index + "@bar.com";
        obj.setEmail(email);
    }

    public void setFirstname(Person obj, int index) {
        String firstname = "firstname_" + index;
        if (firstname.length() > 100) {
            firstname = firstname.substring(0, 100);
        }
        obj.setFirstname(firstname);
    }

    public void setJoined(Person obj, int index) {
        Date joined = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(
                Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(
                Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(
                Calendar.SECOND)
                + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setJoined(joined);
    }

    public void setLastname(Person obj, int index) {
        String lastname = "lastname_" + index;
        if (lastname.length() > 100) {
            lastname = lastname.substring(0, 100);
        }
        obj.setLastname(lastname);
    }

    public void setNickname(Person obj, int index) {
        String nickname = "nickname_" + index;
        if (nickname.length() > 50) {
            nickname = nickname.substring(0, 50);
        }
        obj.setNickname(nickname);
    }

    public void setPassword(Person obj, int index) {
        String password = "password_" + index;
        if (password.length() > 256) {
            password = password.substring(0, 256);
        }
        obj.setPassword(password);
    }

    public void setPicture(Person obj, int index) {
        String picture = "picture_" + index;
        obj.setPicture(picture);
    }

    public void setRegistrationCode(Person obj, int index) {
        String registrationCode = "registrationCode_" + index;
        obj.setRegistrationCode(registrationCode);
    }

    public void setWebsite(Person obj, int index) {
        String website = "website_" + index;
        if (website.length() > 256) {
            website = website.substring(0, 256);
        }
        obj.setWebsite(website);
    }

    public Person getSpecificPerson(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        Person obj = data.get(index);
        Long id = obj.getId();
        return Person.findPerson(id);
    }

    public Person getRandomPerson() {
        init();
        Person obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return Person.findPerson(id);
    }

    public boolean modifyPerson(Person obj) {
        return false;
    }

    public void init() {
        int from = 0;
        int to = 10;
        data = Person.findPersonEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'Person' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }

        data = new ArrayList<Person>();
        for (int i = 0; i < 10; i++) {
            Person obj = getNewTransientPerson(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage())
                            .append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }

    public void setEmailCode(Person obj, int index) {
        String emailCode = "foo" + index + "@bar.com";
        obj.setEmailCode(emailCode);
    }

    public void setTempEmail(Person obj, int index) {
        String tempEmail = "foo" + index + "@bar.com";
        obj.setTempEmail(tempEmail);
    }

    public void setToken(Person obj, int index) {
        String token = "token_" + index;
        obj.setToken(token);
    }
}
